package com.zhike.file.imfile.controller;

import cn.hutool.crypto.SecureUtil;
import org.liuxp.minioplus.api.StorageService;
import org.liuxp.minioplus.api.model.vo.CompleteResultVo;
import org.liuxp.minioplus.api.model.vo.FileCheckResultVo;
import org.liuxp.minioplus.api.model.vo.FileMetadataInfoVo;
import org.liuxp.minioplus.extension.context.Response;
import org.liuxp.minioplus.extension.dto.FileCompleteDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartRequest;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@RestController
public class TestController {
    @Autowired
    private StorageService storageService;

    @PostMapping("/upload")
    public void minio(HttpServletRequest request) throws IOException {
        MultipartFile file = ((MultipartRequest) request).getFile("file");
        storageService.createFile(file.getOriginalFilename(),false, "admin",file.getInputStream());
    }

    public Response<Object> complete(String fileKey, FileCompleteDTO fileCompleteDTO) {

        CompleteResultVo completeResultVo = storageService.complete(fileKey, fileCompleteDTO.getPartMd5List(), "admin");

        return Response.success(completeResultVo);
    }

    @GetMapping("/download")
    public String download(String key) throws IOException {
        return storageService.image(key,"admin");
    }
}

